/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
    	if (head == NULL || head->next == NULL) {
			return head;
		}
		ListNode* i = head;
		ListNode* new_head = NULL;
		ListNode* cur = NULL;

		while (i) {
			ListNode* j = i;
			int count = 0;
			while (j && j->val == i->val) {
				j = j->next;
				++count;
			}
			if (count == 1) {
				if (new_head == NULL) {
					new_head = i;
					cur = i;
				} else {
					cur->next = i;
                    cur = cur->next;
				}
			}
			i = j;
		}
		if (cur) {
			cur->next = NULL;
		}
		return new_head;

    }
};